package com.telenav.techtalk;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas.VertexMode;
import android.graphics.Shader.TileMode;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

/**
 * 
 * @author yxyao
 *
 */
public class VerticesActivity extends Activity {

	private float[] vertices;
	private float[] textures;
	private Bitmap bmp;
	private Paint paint;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		bmp =BitmapFactory.decodeResource(getResources(), R.drawable.hawks_bay);
		int width = bmp.getWidth();
		int height = bmp.getHeight();
		vertices = new float[]{width/2, height/2,0.0f,0.0f,width,0,width,height,0,height, 0.0f,0.0f};
		textures = new float[vertices.length];
		System.arraycopy(vertices, 0, textures, 0, vertices.length);
		setContentView(new VerticesView(this),new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setShader(new BitmapShader(bmp, TileMode.CLAMP, TileMode.CLAMP));
	}
	
	class VerticesView extends View{

		public VerticesView(Context context) {
			super(context);
			setClickable(true);
		}
		
		protected void onDraw(android.graphics.Canvas canvas) {
			canvas.drawVertices(VertexMode.TRIANGLE_FAN, vertices.length, vertices, 0, textures, 0, null, 0, null, 0, 0, paint);
		};
		
		public boolean onTouchEvent(MotionEvent event) {
			if(event.getAction() == MotionEvent.ACTION_DOWN
					||event.getAction() == MotionEvent.ACTION_UP){
				textures[0] = event.getX();
				textures[1] = event.getY();
			}
			vertices[0] = event.getX();
			vertices[1] = event.getY();
			invalidate();
			return super.onTouchEvent(event);
		}
	}
}
